{% extends 'dns_manager/base.html' %}
{% load crispy_forms_tags %}

{% block title %}添加DNS记录 - {{ domain.name }} - DNS管理系统{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-primary text-white">
                <h4 class="mb-0">
                    <i class="bi bi-plus-circle me-2"></i>添加DNS记录
                </h4>
                <p class="mb-0 mt-1 opacity-75">为 {{ domain.name }} 添加新的解析记录</p>
            </div>
            
            <div class="card-body p-4">
                <form method="post" id="dnsRecordForm">
                    {% csrf_token %}
                    
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">主机记录 *</label>
                            {{ form.name }}
                            {% if form.name.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.name.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">
                                例如：www、@、*、mail
                            </small>
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">记录类型 *</label>
                            {{ form.record_type }}
                            {% if form.record_type.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.record_type.errors.0 }}
                                </div>
                            {% endif %}
                        </div>
                        
                        <div class="col-12">
                            <label class="form-label fw-semibold">记录值 *</label>
                            {{ form.value }}
                            {% if form.value.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.value.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted" id="valueHelp">
                                请输入对应记录类型的值
                            </small>
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">TTL *</label>
                            {{ form.ttl }}
                            {% if form.ttl.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.ttl.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">
                                生存时间（秒），范围：60-86400
                            </small>
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">优先级</label>
                            {{ form.priority }}
                            {% if form.priority.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.priority.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted" id="priorityHelp">
                                仅MX记录需要设置
                            </small>
                        </div>
                        
                        <div class="col-12">
                            <div class="form-check">
                                {{ form.is_active }}
                                <label class="form-check-label fw-semibold" for="{{ form.is_active.id_for_label }}">
                                    启用此记录
                                </label>
                            </div>
                        </div>
                    </div>
                    
                    <hr class="my-4">
                    
                    <!-- DNS记录类型说明 -->
                    <div class="alert alert-info">
                        <h6 class="alert-heading">
                            <i class="bi bi-info-circle me-2"></i>记录类型说明
                        </h6>
                        <div class="row g-3 small">
                            <div class="col-md-6">
                                <strong>A记录:</strong> 将域名指向IPv4地址<br>
                                <strong>AAAA记录:</strong> 将域名指向IPv6地址<br>
                                <strong>CNAME记录:</strong> 将域名指向另一个域名
                            </div>
                            <div class="col-md-6">
                                <strong>MX记录:</strong> 邮件交换记录<br>
                                <strong>TXT记录:</strong> 文本记录，用于验证等<br>
                                <strong>NS记录:</strong> 域名服务器记录
                            </div>
                        </div>
                    </div>
                    
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check me-1"></i>保存记录
                        </button>
                        <a href="{% url 'domain_records' domain.id %}" class="btn btn-outline-secondary">
                            <i class="bi bi-x me-1"></i>取消
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    const recordTypeSelect = document.querySelector('select[name="record_type"]');
    const valueHelp = document.getElementById('valueHelp');
    const priorityHelp = document.getElementById('priorityHelp');
    const priorityField = document.querySelector('input[name="priority"]');
    
    function updateHelpText() {
        const recordType = recordTypeSelect.value;
        let helpText = '请输入对应记录类型的值';
        let priorityVisible = false;
        
        switch(recordType) {
            case 'A':
                helpText = '例如：192.168.1.1 (IPv4地址)';
                break;
            case 'AAAA':
                helpText = '例如：2001:db8::1 (IPv6地址)';
                break;
            case 'CNAME':
                helpText = '例如：www.example.com (目标域名)';
                break;
            case 'MX':
                helpText = '例如：mail.example.com (邮件服务器域名)';
                priorityVisible = true;
                break;
            case 'TXT':
                helpText = '例如：v=spf1 include:_spf.google.com ~all';
                break;
            case 'NS':
                helpText = '例如：ns1.example.com (域名服务器)';
                break;
        }
        
        valueHelp.textContent = helpText;
        
        // 显示或隐藏优先级字段
        const priorityContainer = priorityField.closest('.col-md-6');
        if (priorityVisible) {
            priorityContainer.style.display = 'block';
            priorityHelp.textContent = 'MX记录优先级，数值越小优先级越高';
        } else {
            priorityContainer.style.display = 'none';
            priorityField.value = '0';
        }
    }
    
    // 初始化和监听变化
    updateHelpText();
    recordTypeSelect.addEventListener('change', updateHelpText);
    
    // 表单验证
    const form = document.getElementById('dnsRecordForm');
    form.addEventListener('submit', function(e) {
        const recordType = recordTypeSelect.value;
        const value = document.querySelector('textarea[name="value"]').value.trim();
        
        // 简单的格式验证
        if (recordType === 'A') {
            const ipv4Pattern = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
            if (!ipv4Pattern.test(value)) {
                alert('请输入有效的IPv4地址');
                e.preventDefault();
                return;
            }
        } else if (recordType === 'MX') {
            const priority = document.querySelector('input[name="priority"]').value;
            if (!priority || priority < 0) {
                alert('MX记录必须设置优先级');
                e.preventDefault();
                return;
            }
        }
    });
});
</script>
{% endblock %}